//
//  PermissionLayer.cpp
//  client
//
//  Created by ming on 2019/1/24.
//

#include "PermissionLayer.hpp"
using namespace cocos2d;
using namespace cocos2d::ui;
#ifdef USE_EMACDN
#include "EmaOverseaSdkApi.hpp"
#endif
#include "MessageDialog.h"
#include "LogoScene.h"
#include "Localization.h"

Scene* PermissionLayer::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = PermissionLayer::create();
    // add layer as a child to scene
    scene->addChild(layer);
    // return the scene
    return scene;
}


PermissionLayer::PermissionLayer(){
    
}

PermissionLayer::~PermissionLayer(){
    if(_listener != nullptr){
        Director::getInstance()->getEventDispatcher()->removeEventListener(_listener);
        _listener = nullptr;
    }
}


bool PermissionLayer::init()
{
    if (!Layer::init())
    {
        return false;
    }
    auto rootNode = CSLoader::createNode("ui2/login/ACCTSet.csb");
    if(rootNode){
        this->addChild(rootNode);
        cocos2d::ui::Helper::doLayout(rootNode);
        
        auto panelNode = rootNode->getChildByName("Panel");
        if(panelNode){
           
            Button * btn = dynamic_cast<Button*>(panelNode->getChildByName("Button"));
            if(btn){
                btn->addClickEventListener([this](Ref* sender){
                    if(!EmaOverseaSdkApi::checkPermissions()){
                        EmaOverseaSdkApi::requestPermissions();
                    }else{
                        gotoLogoScene();
                    }
                });
            }
        }
        _listener = Director::getInstance()->getEventDispatcher()->addCustomEventListener("CHANNEL_REQUEST_PREMISSIONS_EVENT", [this](cocos2d::EventCustom* event){
            this->_onPermissionResult(event);
        });
    }
    return true;
}

void PermissionLayer::gotoLogoScene(){
//    auto newScene = Scene::create();
//    auto layer = InstallLayerEMA::create();
//    layer->setName("HotFixScene");
//    newScene->addChild(layer);
    Scene * scene=TransitionFade::create(1.0f, LogoScene::createScene(false),Color3B::BLACK);
    Director::getInstance()->replaceScene(scene);
}

void PermissionLayer::_onPermissionResult(cocos2d::EventCustom* event){
    std::string s = event->getUserStr();
    if(s == "success"){
        gotoLogoScene();
    }else{
        MessageDialog* dialog = MessageDialog::create();
        dialog->setTwoButton(true);
//        char msg[512];
//        sprintf(msg, Localization::getInstance()->getStr("%s").c_str(), )
//        std::string msgContent = "Granted Premission " + Localization::getInstance()->getStr(s) + " Fail!";
        std::string msgContent = "Granted Premission " + Localization::getInstance()->getStr(s) + " Fail!";
        dialog->setMessage(msgContent.c_str());
        dialog->setConfirmButton(Localization::getInstance()->getStr("CommonBtnAppSetting").c_str());
        dialog->setCancelButtonStr(Localization::getInstance()->getStr("CommonBtnCancel").c_str());
        dialog->setConfirmCallback([this](MessageDialog* sender){
            sender->removeFromParent();
            EmaOverseaSdkApi::goAppSetting();
        });
        dialog->setCancelCallback([this](MessageDialog* sender){
            sender->removeFromParent();
        });
        this->addChild(dialog);
        CCLOG("GRANTED Permission fail");
    }
}
